//登录获取用户名
var rename = tool.getCookie('rename')
var user_name = tool.getCookie("username")
if(rename || user_name){
    var res = rename || user_name
    $(".nav .a_login").css("display","none").next().css("display","none")
    $(".nav .a_register").css("display","none").next().css("display","none")
    $(".users .a_login").css("display","none")
    $(".users .a_register").css("display","none")
    $(".users").html(res).css("cursor","pointer")
    $(".user_box").css("display","block")
    $(".user_id").text(res)
}
$(".exit").click(function(){
    tool.setCookie('rename',$('#usname').val(),-1000)
    tool.setCookie('username',$('#usname').val(),-1000)
    location.href="./"
})
$(".close").click(function(){
    $("#ad").remove()
})

$(".cart").click(function(){
    location.href="./shopping.html"
})
var loadindex = layer.load('0',{
    shade:[0.5,"#000"]
})
//跳楼导航
var lastmenu=$("#menu li").last()
var num = 0;
var arr =[694,990,1938,2960,3000];
$(window).scroll(function(){
    if($(window).scrollTop()>200){
        $("#menu").css("display","block")
    }else{
        $("#menu").css("display","none")
    }
    if($(window).scrollTop()>1300){
        $(lastmenu).css("display","block")
    }else{
        $(lastmenu).css("display","none")
    }
    if($(window).scrollTop()>600){
        console.log(1)
        $("#hide_top").slideDown(300)
    }else{
        $("#hide_top").slideUp(300)
    }
    for (var i = 0; i < arr.length - 1; i++) {
        if ($(window).scrollTop() >= arr[i] && $(window).scrollTop() < arr[i + 1]) {
            num = i;
            break;
        }
    }
    $("#menu li").eq(num).addClass("current").siblings().removeClass("current")
    $("#menu li").click(function(){
        var target=arr[$(this).index()]
        console.log(target)
        $("html,body").stop().animate({
            scrollTop:target
           
        },600)
    })
    $(lastmenu).click(function(){
        $("html,body").stop().animate({
            scrollTop:0
        },600)
    })
})
// function animate(target) {
//     //scrollTop属性缓动
//     clearInterval(document.timer);
//     document.timer = setInterval(function () {
//         //1 获取元素当前位置
//         var current = getScroll();
//         //2 计算速度
//         var speed = (target - current) / 10;
//         speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
//         //3 计算元素当前位置
//         current = current + speed;
//         //4 判断是否到达目标
//         if (current == target) {
//             clearInterval(document.timer);
//         }
//         //5 定位元素
//         setScroll(current)
//     }, 10)
// }
// function setScroll(value) {
//     if (document.body.scrollTop) {
//         document.body.scrollTop = value;
//     } else {
//         document.documentElement.scrollTop = value;
//     }
// }
// function getScroll() {
//     return document.body.scrollTop + document.documentElement.scrollTop;
// }


//轮播图
var index=1;
for(var i=0;i<$(".swiper .swiper-slide").length;i++){
    $(".swiperbox ol").append($('<li></li>'))
}
$('.swiperbox ol li').eq(0).addClass('active')
var $first = $('.swiperbox .swiper .swiper-slide').first().clone()
$('.swiperbox .swiper').prepend($('.swiperbox .swiper .swiper-slide').last().clone())
$('.swiperbox .swiper').append($first)
$('.swiperbox .swiper').css({
    width:$('.swiperbox .swiper .swiper-slide').length * $first.width() + "px",
    left:-index * $first.width() + "px"
})
var flag = true
$('.swiperbox>a.rightBtn').click(function(){
    if(!flag){
        return false;
    }
    flag = false
    index++;
    $('.swiperbox .swiper').animate({
        left:-index * $first.width()
    },500,'linear',function(){
        if(index === $('.swiperbox .swiper .swiper-slide').length-1){
            index = 1;
            $('.swiperbox .swiper').css({
                left:-index * $first.width() + "px"
            })
        }
        $('.swiperbox ol li').eq(index-1).addClass('active').siblings().removeClass('active')
        flag = true
    })
    return false;
})

$('.swiperbox>a.leftBtn').click(function(){
    if(!flag){
        return false;
    }
    flag = false
    index--;
    $('.swiperbox .swiper').animate({
        left:-index * $first.width()
    },500,'linear',function(){
        if(index === 0){
            index = $('.swiperbox .swiper .swiper-slide').length-2;
            $('.swiperbox .swiper').css({
                left:-index * $first.width() + "px"
            })
        }
        $('.swiperbox ol li').eq(index-1).addClass('active').siblings().removeClass('active')
        flag = true
    })
    return false;
})


$('.swiperbox ol li').click(function(){
    if(!flag){
        return false;
    }
    flag = false
    index = $(this).index()+1;
    $('.swiperbox .swiper').animate({
        left:-index * $first.width()
    },500,'linear',function(){
        if(index === 0){
            index = $('.swiperbox .swiper .swiper-slide').length-2;
            $('.swiperbox .swiper').css({
                left:-index * $first.width() + "px"
            })
        }
        $('.swiperbox ol li').eq(index-1).addClass('active').siblings().removeClass('active')
        flag = true
    })
    return false;
})

var timerId = setInterval(function(){
    if(!flag){
        return false;
    }
    flag = false
    index++;
    $('.swiperbox .swiper').animate({
        left:-index * $first.width()
    },500,'linear',function(){
        if(index === $('.swiperbox .swiper .swiper-slide').length-1){
            index = 1;
            $('.swiperbox .swiper').css({
                left:-index * $first.width() + "px"
            })
        }
        $('.swiperbox ol li').eq(index-1).addClass('active').siblings().removeClass('active')
        flag = true
    })
},500)

$('.swiperbox').hover(function(){
    clearInterval(timerId)
},function(){
    timerId = setInterval(function(){
        if(!flag){
            return false;
        }
        flag = false
        index++;
        $('.swiperbox .swiper').animate({
            left:-index * $first.width()
        },500,'linear',function(){
            if(index === $('.swiperbox .swiper .swiper-slide').length-1){
                index = 1;
                $('.swiperbox .swiper').css({
                    left:-index * $first.width() + "px"
                })
            }
            $('.swiperbox ol li').eq(index-1).addClass('active').siblings().removeClass('active')
            flag = true
        })
    },500)
})

//按钮点击
console.log($(".a_login"))
$(".a_login").click(function(){
    location.href="./login.html"
})
$(".a_register").click(function(){
    location.href="./register.html"
})

//侧边栏
$.ajax({
    url:"../php/category.php",
    dataType:"json",
    
}).then(res=>{
        var {data}=res;
        var first=data.filter(function(item){  
            return  item.pid==0
        })
        // var first = data.filter(item=>item.pid==0)
        handlerData(data,first)
        var dom=render(first);
        document.querySelector(".main_inn>ul").innerHTML=dom;
        // console.log($('.category>li'))
        $('.category>li').mouseenter(function(){
            $(this).children('ul').stop().show().parent().siblings().children('ul').stop().hide()
            $(this).children('ul').children().children('ul').stop().hide()
        })
        $('.category>li').mouseleave(function(){
            $(this).children('ul').stop().hide()
        })
        $('.category>li>ul>li').mouseenter(function(){
            console.log($(this).children('ul'))
            $(this).children('ul').stop().show().parent().siblings().children('ul').stop().hide()
        })
        $('.category>li>ul>li').mouseleave(function(){
            $(this).children('ul').stop().hide()
        })
        return $.ajax({
            url:"../php/goods.php",
            data:{pid:1191},
            dataType:"json",
        })
}).then(res=>{
    console.log(111)
    var {data} = res;
    str = "";
    data.forEach(item=>{
        str += `
        <dl>
        <a href="detail.html?id=${item.id}">
            <dt><img src="${item.img}" alt="" title="${item.name}"></dt>
            <dd><p>${item.name}</p><b><span>￥</span>${item.price}</b></dd>
         </a>
         </dl>
        `
    })
    // $(".list").html(str)
    return $.ajax({
        url:"../php/goods.php",
        data:{pid:1212},
        dataType:"json",
    })
}).then(res=>{
    console.log(222)
    var {data}=res;
    data.forEach(item=>{
        str+=`
        <dl>
        <a href="detail.html?id=${item.id}">
            <dt><img src="${item.img}" alt="" title="${item.name}"></dt>
            <dd><p>${item.name}</p><b><span>￥</span>${item.price}</b></dd>
         </a>
         </dl>
        `
    })
    $(".list").html(str)
    layer.close(loadindex)
})

function handlerData(data,first){
    for(var i=0;i<first.length;i++){
        var res = data.filter(function(item){
            return item.pid == first[i].id
        })
        if(res.length){
            first[i].children = res;
            handlerData(data,first[i].children)
        }
    }
}
function render(first){
    str = "";
    for(var i=0;i<first.length;i++){
        str +='<li><a href="list.html?cat='+first[i].id+'">'+first[i].name+'</a>'
        if(first[i].children){
            str+="<ul>";
            str+=render(first[i].children);
            str+="</ul>"
        }
        str+="</li>"
    }
    return str;
}



(function () {
    var begin = new Date(2021, 2, 17, 23, 0, 0);//2021年1月7号20：00：00（如果倒数第三个是24，则是0点）
    var target_hour = begin.getHours();
    // console.log(target_hour)
    if (target_hour < 10) {
        target_hour = "0" + target_hour;
    }
    var target_minutes = begin.getMinutes();
    // console.log(target_minutes)
    if (target_minutes < 10) {
        target_minutes = "0" + target_minutes;
    }
    $(".miaosha").children("span").html(`${target_hour} : ${target_minutes}`)

    setInterval(function () {
        var time = new Date();
        var time_rest = begin - time;
        time_rest = time_rest / 1000;
        var hours = parseInt(time_rest / 3600);
        if (hours < 10) {
            $(".hours").html(`0${hours}`);
        } else {
            $(".hours").html(`${hours}`);
        }

        var minutes = parseInt((time_rest - hours * 3600) / 60);
        if (minutes < 10) {
            $(".minutes").html(`0${minutes}`);
        } else {
            $(".minutes").html(`${minutes}`);
        }

        var seconds = parseInt(time_rest - minutes * 60 - hours * 3600);
        if (seconds < 10) {
            $(".seconds").html(`0${seconds}`);
        } else {
            $(".seconds").html(`${seconds}`);
        }
    }, 1000)
})()